package com.cx.java;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cx.java.JdbcUtils;

import cx.cx.pojo.Car;

/**
 * Servlet implementation class Query
 */
@WebServlet("/Query")
public class QueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	
		String searchstr1 = request.getParameter("searchstr1");
		JdbcUtils ju = JdbcUtils.getInstance();
		//数据库表名待定
		String sql = "Select * from car where carname like'%"+ searchstr1+"%';";
		System.out.println(sql);
		// good里面有一条数据，因为是根据id查找的
		List<Car> list = ju.executeQuery(Car.class, sql);
		
		HttpSession session = request.getSession();
		session.setAttribute("list",list);
		
		
		if(!list.isEmpty()) {
			// 成功，跳转显示页面？	
			response.sendRedirect("/car/query.jsp");
		}else {
				// 失败，添加失败?		
				response.sendRedirect("/car/add.jsp");
			}
		
		
		
		
		
		
	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
